package com.digdes.calendar.gwt.client.widget;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DateBox;

import java.util.Date;

/**
 * User: Dmitry
 * Date: 17.05.12
 * Time: 16:44
 */
public class MainPanel extends VerticalPanel {
    private String[] timesCell = new String[]{"9-00", "10-00", "11-00", "12-00",
            "13-00", "14-00", "15-00", "16-00", "17-00", "18-00"};
    private int blockcell = timesCell.length - 1;

    public FlexTable persons = new FlexTable();
    public FlexTable schedulerHeader = new FlexTable();
    public AbsolutePanel schedulerPanel = new AbsolutePanel();
    public MainScheduler schedulerMain = new MainScheduler();
    public DateBox dateStart;
    public DateBox dateFinish;
    public ListBox timeStart;
    public ListBox timeFinish;
    public Button addUser = new Button();
    public Button addPlace = new Button();


  //  public Button saveEvent = new Button();
    public Button goEventCreate = new Button();


  //  public DialogBox errorDialog = new DialogBox();
    private int vsize = 20;
    private int hsize = 0;
    private int curLine = 1;
    private int countday;

    public MainPanel() {
        HorizontalPanel hh = new HorizontalPanel();
        VerticalPanel vv = new VerticalPanel();
        VerticalPanel vv1 = new VerticalPanel();
        vv1.setSize("", "20px");
        vv1.add(schedulerHeader);
        vv.add(vv1);
        vv.setSize("100px", "200px");

        schedulerPanel.add(schedulerMain);
        schedulerMain.setSchedulerPanel(schedulerPanel);
        vv.add(schedulerPanel);
        // vv.add(schedulerMain);
        hh.add(persons);
        hh.add(vv);

        persons.setStyleName("persons");
        schedulerHeader.setStyleName("scheduler");
        schedulerMain.setStyleName("scheduler");


        dateStart = new DateBox();
        dateStart.setValue(new Date());
        dateFinish = new DateBox();
        dateFinish.setValue(new Date());
        DateTimeFormat dateFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_LONG);
        dateFinish.setFormat(new DateBox.DefaultFormat(dateFormat));
        dateStart.setFormat(new DateBox.DefaultFormat(dateFormat));
        timeStart = new ListBox();
        timeFinish = new ListBox();
        for (String aTimesCell : timesCell) {
            timeStart.addItem(aTimesCell);
            timeFinish.addItem(aTimesCell);
        }
        timeFinish.setSelectedIndex(timesCell.length - 1);
        AbsolutePanel timePanel = new AbsolutePanel();
        timePanel.setSize("280px", "130px");
        timePanel.add(new HTML("Начало события"), 30, 0);
        timePanel.add(dateStart, 30, 20);
        timePanel.add(timeStart, 200, 20);
        timePanel.add(new HTML("Окончание события"), 30, 50);
        timePanel.add(dateFinish, 30, 70);
        timePanel.add(timeFinish, 200, 70);

        countday = (int) ((dateFinish.getValue().getTime() - dateStart.getValue().getTime()) / 86400000) + 1;
        hsize = blockcell * countday;


        addUser.setText("+Участник");
        addPlace.setText("+Место");
        //saveEvent.setText("Сохранить");
        goEventCreate.setText("Ок");
        goEventCreate.setSize("100px","40px");


        this.add(hh);
        AbsolutePanel buttonPanel = new AbsolutePanel();
        buttonPanel.setSize("900px","110px");
        buttonPanel.add(addUser, 30, 20);
        buttonPanel.add(addPlace,30,60);
        buttonPanel.add(goEventCreate,330,20);

        this.add(buttonPanel);
        this.add(timePanel);
    }
    public void refreshTime(int i){
        if (i==0){
            schedulerMain.RefreshTimeBorders(0,timeStart.getSelectedIndex(), vsize, blockcell, countday);
        }   else{
            schedulerMain.RefreshTimeBorders(1,timeFinish.getSelectedIndex(), vsize, blockcell, countday);
        }
    }

    public int getVsize() {
        return this.vsize;
    }

    public void setVsize(int vsize) {
        this.vsize = vsize;
    }

    public int getHsize() {
        return this.hsize;
    }

    public void setHsize(int hsize) {
        this.hsize = hsize;
    }

    public int getCurLine() {
        return this.curLine;
    }

    public void setCurLine(int curLine) {
        this.curLine = curLine;
    }

    public int getCountday() {
        return this.countday;
    }

    public void setCountday(int countday) {
        this.countday = countday;
    }

}
